package data;

import json2object.JsonParser;
import hl.UI;
import haxe.SysTools;
import sys.io.File;
import haxe.Json;
import tree.Root;
import tree.LeafKind;



//一系列画的合集
class Data_Book extends Data {
    var pages : Array<InnerData_Canvas>;
    public var curpage(default, null) : Int;
    public var pageCount(get, null) : Int;
    

    public function new() {
        super();
        this.pages = new Array<InnerData_Canvas>();
        this.pages[0] = new InnerData_Canvas();
        this.curpage = 0;
    } 

    public override function OnEnter(last:LeafKind) {
        super.OnEnter(last);
        data_canvas.SetCanvas(pages[curpage]);
        Root.instance.AddMsg(RefreshTimeline);
    }

    private function get_pageCount() {
        return pages.length;
    }

    public function AddPage() {
        pages.push(new InnerData_Canvas());
        Root.instance.AddMsg(RefreshTimeline);
        return pageCount - 1;
    }

    public function RemovePage(page) {
        var data = pages[page];
        pages.remove(data);
        Root.instance.AddMsg(RefreshTimeline);
        return data;
    }

    public function ChangeCurPage(page) {
        var oPage = curpage;
        curpage = page;
        data_canvas.SetCanvas(pages[curpage]);
        Root.instance.AddMsg(RefreshTimeline);
        return oPage;
    }

    public function GetPagePixels(page) {
        return pages[page].GetPixels();
    }

    //获取某一页的画面
    public function GetPageColors(page) {
        return pages[page].GetColors();
    }

    //切换下一页
    public function NextPage() {
        var page = (curpage + 1) % pageCount;
        ChangeCurPage(page);
    }
    

    public override function Save(data:InnerData_SL) {
        data.pages = pages;
        data.curpage = curpage;
    }

    public override function Load(data:InnerData_SL) {
        pages = data.pages;
        curpage = data.curpage;
        OnEnter(null);   
    }

}